﻿//------------------------------------------------------------------------------
// <copyright file="MvcApplication.cs" company="David Martos">
//     Copyright (c) David Martos.  All rights reserved.
// </copyright>
// <summary>HTTP Application class</summary>
//  ----------------------------------------------------------------------------
namespace Dissemination.MVC
{
    using System;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using log4net;
    using System.Globalization;
    using System.Threading;

    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    /// <summary>
    /// HTTP Application class
    /// </summary>
    public class MvcApplication : HttpApplication
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(MvcApplication));

        /// <summary>
        /// Registers the list of available routes for the MVC pattern
        /// </summary>
        /// <param name="routes">Collection of routes</param>
        public static void RegisterRoutes(RouteCollection routes)
        {
            try
            {
                if (log.IsDebugEnabled) log.Debug("Entering RegisterRoutes...");

                routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

                routes.MapRoute(
                    "TargetAreas", // Route name
                    "Program/{programid}/TargetAreas/{action}/{id}", // URL with parameters
                    new { controller = "TargetArea", action = "Details", programid = UrlParameter.Optional, id = UrlParameter.Optional } // Parameter defaults
                );

                routes.MapRoute(
                    "Reporters", // Route name
                    "Program/{programid}/TargetArea/{targetareaid}/Reporters/{action}/{id}", // URL with parameters
                    new { controller = "Reporter", action = "Details", programid = UrlParameter.Optional, targetareaid = UrlParameter.Optional, id = UrlParameter.Optional } // Parameter defaults
                );

                routes.MapRoute(
                    "Surveys", // Route name
                    "Program/{programid}/Surveys/{action}/{id}", // URL with parameters
                    new { controller = "Survey", action = "Details", programid = UrlParameter.Optional, id = UrlParameter.Optional } // Parameter defaults
                );

                routes.MapRoute(
                    "Participants", // Route name
                    "Seminar/{seminarid}/Participants/{action}/{id}", // URL with parameters
                    new { controller = "Participant", action = "Details", seminarid = UrlParameter.Optional, id = UrlParameter.Optional } // Parameter defaults
                );

                routes.MapRoute(
                    "TestResults", // Route name
                    "Seminar/{seminarid}/TestResults/{action}/{id}", // URL with parameters
                    new { controller = "TestResult", action = "Index", seminarid = UrlParameter.Optional, id = UrlParameter.Optional } // Parameter defaults
                );

                routes.MapRoute(
                    "Assessments", // Route name
                    "Seminar/{seminarid}/Assessments/{action}/{id}", // URL with parameters
                    new { controller = "Assessment", action = "Index", seminarid = UrlParameter.Optional, id = UrlParameter.Optional } // Parameter defaults
                );

                routes.MapRoute(
                    "FollowUps", // Route name
                    "Seminar/{seminarid}/FollowUps/{action}/{id}", // URL with parameters
                    new { controller = "FollowUp", action = "Index", seminarid = UrlParameter.Optional, id = UrlParameter.Optional } // Parameter defaults
                );

                routes.MapRoute(
                    "Default", // Route name
                    "{controller}/{action}/{id}", // URL with parameters
                    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
                );

                if (log.IsDebugEnabled) log.Debug("Leaving RegisterRoutes...");
            }
            catch (Exception ex)
            {
                if (log.IsErrorEnabled) log.Error("Error in RegisterRoutes", ex);
            }

        }

        /// <summary>
        /// Application_Start method is fired everytime the application pool is initiated
        /// </summary>
        protected void Application_Start()
        {
            try
            {
                //log4net.Config.XmlConfigurator.Configure();

                if (log.IsDebugEnabled) log.Debug("Entering Application_Start...");

                AreaRegistration.RegisterAllAreas();

                RegisterRoutes(RouteTable.Routes);

                //CultureInfo newCulture = (CultureInfo)System.Threading.Thread.CurrentThread.CurrentCulture.Clone();
                //newCulture.DateTimeFormat.ShortDatePattern = "dd-MM-yyyy";
                //newCulture.DateTimeFormat.DateSeparator = "-";
                //Thread.CurrentThread.CurrentCulture = newCulture; 


                if (log.IsDebugEnabled) log.Debug("Leaving Application_Start...");
            }
            catch (Exception ex)
            {
                if (log.IsErrorEnabled) log.Error("Error on Application_Start", ex);
            }

        }
    }
}